import axios from 'axios';
import {BannerUrl, UsualSearchUrl, UsualWebSiteUrl} from '../../../common/API';
import {
    change_banner_index, project_update_tag,
    SAVE_BANNER_DATA, save_project_data, update_article_choice, update_article_data,
    update_banner_timer,
    update_search_data, update_tag,
    update_website_data
} from "../contants";


export const getBannerList = () => {
    return (dispatch) => {
        axios.get(BannerUrl)
            .then((res) => {
                const data = res.data.data;
                const action = saveBannerList(data);
                dispatch(action);
            });
    }
};

//保存banner 数据
const saveBannerList = (result) => ({
    type: SAVE_BANNER_DATA,
    data: result
});

//改变banner 轮播的当前和上一个的值
export const changeBannerIndex = (lastIndex, currentIndex) => ({
    type: change_banner_index,
    lastIndex,
    currentIndex
});

//改变banner 轮播的当前和上一个的值
export const updateBannerTimer = (bannerTimer) => ({
    type: update_banner_timer,
    bannerTimer: bannerTimer
});


export const getWebSiteData = () => {
    return (dispatch) => {
        axios.get(UsualWebSiteUrl)
            .then((res) => {
                const data = res.data.data;
                const action = updateWebsiteData(data);
                dispatch(action);
            })
    }
};

const updateWebsiteData = (res) => (
    {
        type: update_website_data,
        data: res
    }
);

export const getSearchData = () => {
    return (dispatch) => {
        axios.get(UsualSearchUrl)
            .then((res) => {
                const data = res.data.data;
                const action = updateSearchData(data);
                dispatch(action);
            })
    }
};

const updateSearchData = (result) => (
    {
        type: update_search_data,
        data: result

    }
);

export const getArticleData = (currentIndex) => {

    return (dispatch) => {
        const url = `/article/list/${currentIndex}/json`;
        axios.get(url)
            .then((res) => {
                const data = res.data.data.datas;
                const action = updateArticleData(data, currentIndex);
                dispatch(action);

                const updateTag = changeUpdateTag(true);
                dispatch(updateTag);

            }).catch((exception) => {
            console.log("请求文章列表出现错误");
            console.log(exception);
        })
    };
};

const updateArticleData = (result, index) => ({
    type: update_article_data,
    data: result,
    index: index
});

export const changeUpdateTag = (bool) => ({
    type: update_tag,
    bool: bool,
});

export const updateTitleChoice = (index) => ({
    type: update_article_choice,
    titleIndex: index
});


export const requestProjectData = (index) => {
    return (dispatch) => {
        axios.get(`/article/listproject/${index}/json`)
            .then((res) => {
                const datas = res.data.data.datas;
                const action = saveProjectData(index, datas);
                dispatch(action);

                const tagAction = updateTagStatus(true);
                dispatch(tagAction);
            })
    }
};

const saveProjectData = (index, data) => ({
    type: save_project_data,
    index: index,
    data: data
});


export const updateTagStatus = (bool) => ({
    type: project_update_tag,
    bool: bool
});

